﻿-- Author      : Kurapica

--------------------------
-- WidgetList	        --
--------------------------
do
	IGAS:NewAddon("IGAS_Studio")
    
	local _WidgetName = "WidgetList"
	local _Base = "Frame"
	
	local selBtn
	
	------------------------------------------------------
	---------------------  WidgetButton   -----------
	------------------------------------------------------
	local WidgetButton
	do	    
	    local _WidgetName = "WidgetButton"
	    local _Base = "Button"

	    -- Script		
	    local function OnMouseDown(self)
	        if self.ButtonState == "PUSHED" then
	            self.ButtonState = "NORMAL"
				if selBtn == self then
					selBtn = nil
					self:FireEvent("IGAS_STUDIO_WIDGET_SELECTED", nil)
				end
	        else
				if selBtn and selBtn ~= self then
					selBtn.ButtonState = "NORMAL"
				end
	            self.ButtonState = "PUSHED"
				selBtn = self
				self:FireEvent("IGAS_STUDIO_WIDGET_SELECTED", self.Widget)
	       end
	    end
		
	    -- ScriptType
	    local _ScriptType = {
	    }

	    --    Function
	    local _Function = {
	    }

	    --    Property
	    local _Property = {
			-- ToggleState
			["Widget"] = {
				["Set"] = function(self, widget)
					self.__Widget = widget
					if widget and widget.WidgetName then
						self.Text = widget.WidgetName
					else
						self.Text = ""
					end
				end,

				["Get"] = function(self)
					return self.__Widget
				end,

				["Type"] = IGAS:GetPropertyType("Object"),
			},
	    }

	    local function _New(name, parent)        
	        local btn = CreateFrame("NormalButton", name, parent)

	        btn.Style = "NORMAL"
	        btn.Height = 20
	        btn.Width = 140
	        btn.Locked = true
			btn:SetPoint("LEFT", parent, "LEFT", 10, 0)
			btn:SetPoint("RIGHT", parent, "RIGHT", -10, 0)

	        -- Parent's Settings
	        local index = parent.__WidgetButtonNum or 0

			index = index + 1
			parent.__WidgetButtonNum = index
			btn.ID = index
			
	        btn:StackScript("OnMouseDown", OnMouseDown, true)
			
			return btn
	    end

	    -- Register
	    local _WidgetInfo = {
	        ["WidgetName"] = _WidgetName,
	        ["Base"] = _Base,
	        ["Function"] = _Function,
	        ["Property"] = _Property,
	        ["ScriptType"] = _ScriptType,
	        ["New"] = _New,
	    }

	    WidgetButton = IGAS:NewWidget(_WidgetInfo, WidgetButton)
	end
		
	------------------------------------------------------
	---------------------  WidgetSeparator   -----------
	------------------------------------------------------
	local WidgetSeparator
	do	    
	    local _WidgetName = "WidgetSeparator"
	    local _Base = "Frame"

	    -- Script	
		local function OnMouseDown(self)
			self.Parent.ToggleState = not self.Parent.ToggleState
		end

	    -- ScriptType
	    local _ScriptType = {
	    }

	    --    Function
	    local _Function = {
			-- Dispose, release resource
	        ["Dispose"] = function(self)
				local index = self.ID
				local parent = self.Parent
					
				self.ToggleState = false
				
				-- Dispose Childs
				if self.__WidgetButtonNum and self.__WidgetButtonNum > 0 then
					for i = self.__WidgetButtonNum, 1, -1 do
						if self:GetChild("WidgetButton"..i) then
							self:GetChild("WidgetButton"..i):Dispose()
						end
					end
				end
				
				self:ClearAllPoints()
				
				if parent:GetChild(_WidgetName..(index+1)) then
					if index == 1 then
						parent:GetChild(_WidgetName..(index+1)):SetPoint("TOP", parent, "TOP", -10, 0)
					else
						parent:GetChild(_WidgetName..(index+1)):SetPoint("TOP", parent:GetChild(_WidgetName..(index - 1)), "BOTTOM", 0, 0)
					end
				end
				
				self:Hide()
				
				parent:RemoveChild(self)
				
				if parent.__WidgetSeparatorNum > index then
					for i = index + 1, parent.__WidgetSeparatorNum do
						parent:GetChild(_WidgetName..i).Name = _WidgetName..(i - 1)
					end
				end
				
				parent.__WidgetSeparatorNum = parent.__WidgetSeparatorNum - 1
				
				parent.Height = parent.Height - 26
				
	            -- Call super's dispose
				self:CallMethod(_Base, "Dispose")
	        end,
			
			-- GetToggleState
			["GetToggleState"] = function(self)
				return (self.__ToggleState and true) or false
			end,
			
			-- SetToggleState
			["SetToggleState"] = function(self, flag)
				flag = (flag and true) or false
				
				if self.__ToggleState == flag then
					return
				end
				
				self.__ToggleState = flag
				
				if flag then
					if self.__WidgetButtonNum and self.__WidgetButtonNum > 0 then
						for i = 1, self.__WidgetButtonNum do
							self:GetChild("WidgetButton"..i).Visible = true
							if i == 1 then
								self:GetChild("WidgetButton"..i):SetPoint("TOP", self.TextBtn, "BOTTOM", 0, -6) 
							else
								self:GetChild("WidgetButton"..i):SetPoint("TOP", self:GetChild("WidgetButton"..(i-1)), "BOTTOM", 0, -6)
							end
						end
						
						self.Height = 26 + self.__WidgetButtonNum * 26
						
						self.Parent.Height = self.Parent.Height + self.__WidgetButtonNum * 26
					end
				else
					if self.__WidgetButtonNum and self.__WidgetButtonNum > 0 then
						for i = 1, self.__WidgetButtonNum do
							self:GetChild("WidgetButton"..i).Visible = false
						end
						
						self.Parent.Height = self.Parent.Height - self.__WidgetButtonNum * 26
					end
					
					self.Height = 26
				end
			end,
			
			-- AddWidget
			["AddWidget"] = function(self, widget)
				if self.__WidgetButtonNum and self.__WidgetButtonNum > 0 then
					for i = 1, self.__WidgetButtonNum do
						if self:GetChild("WidgetButton"..i).Widget == widget then
							return self:GetChild("WidgetButton"..i)
						end
					end
				end
				
				local btn = CreateFrame(WidgetButton, nil, self)
				btn.Widget = widget
				
				if self.ToggleState then
					self.Height = self.Height + 26
					self.Parent.Height = self.Parent.Height + 26
				end
				
				if btn.ID == 1 then
					btn:SetPoint("TOP", self.TextBtn, "BOTTOM", 0, -6)
				else
					btn:SetPoint("TOP", self:GetChild("WidgetButton"..(btn.ID-1)), "BOTTOM", 0, -6)
				end
				
				return btn
			end,
			
			-- RemoveWidget
			["RemoveWidget"] = function(self, widget)
				if self.__WidgetButtonNum and self.__WidgetButtonNum > 0 then
					for i = 1, self.__WidgetButtonNum do
						if self:GetChild("WidgetButton"..i).Widget == widget then
							if i == 1 then
								if self.__WidgetButtonNum > 1 then
									self:GetChild("WidgetButton2"):SetPoint("TOP", self.TextBtn, "BOTTOM", 0, -6)
								end
							elseif self.__WidgetButtonNum > i then
								self:GetChild("WidgetButton"..(i+1)):SetPoint("TOP", self:GetChild("WidgetButton"..(i-1)), "BOTTOM", 0, -6)
							end
							
							self:GetChild("WidgetButton"..i):Dispose()
							
							if i < self.__WidgetButtonNum then
								for j = i+1, self.__WidgetButtonNum do
									self:GetChild("WidgetButton"..j).Name = "WidgetButton"..(j-1)
								end
							end
							
							if self.ToggleState then
								self.Height = self.Height - 26
								self.Parent.Height = self.Parent.Height - 26
							end
							
							return
						end
					end
				end
			end,
	    }

	    --    Property
	    local _Property = {		
			-- ToggleState
			["ToggleState"] = {
				["Set"] = function(self, flag)
					self:SetToggleState(flag)
				end,

				["Get"] = function(self)
					return self:GetToggleState()
				end,

				["Type"] = IGAS:GetPropertyType("Boolean"),
			},
			-- Text
			["Text"] = {
				["Set"] = function(self, text)
					self.TextBtn.Text = text
				end,
				
				["Get"] = function(self)
					return self.TextBtn.Text
				end,
				
				["Type"] = IGAS:GetPropertyType("String"),
			},
	    }

	    local function _New(name, parent)-- Base Frame
	        local frame = CreateFrame("Frame", name, parent)
			
			frame.Height = 26
			frame.Width = 180
			frame:SetPoint("LEFT", parent, "LEFT", 0, 0)
			frame:SetPoint("RIGHT", parent, "RIGHT", 0, 0)
			frame.__ToggleState = true
			
	        local btn = CreateFrame("NormalButton", "TextBtn", frame)

	        btn.Style = "GRAY"
	        btn.Height = 20
	        btn.Width = 160
	        btn:SetPoint("TOP", frame, "TOP", 0, 0)
			btn:SetPoint("LEFT", frame, "LEFT", 4, 0)
			btn:SetPoint("RIGHT", frame, "RIGHT", -4, 0)
	        btn.Locked = true

	        -- Parent's Settings
	        local index = parent.__WidgetSeparatorNum or 0
			index = index + 1
			
			if index == 1 then
				frame:SetPoint("TOP", parent, "TOP", -10, 0)
			else
				frame:SetPoint("TOP", parent:GetChild(_WidgetName..(index - 1)), "BOTTOM", 0, 0)
			end
	        
			parent.__WidgetSeparatorNum = index
			parent.Height = parent.Height + 26
			frame.ID = index
			
	        btn:StackScript("OnMouseDown", OnMouseDown, true)
			
			return frame
	    end

	    -- Register
	    local _WidgetInfo = {
	        ["WidgetName"] = _WidgetName,
	        ["Base"] = _Base,
	        ["Function"] = _Function,
	        ["Property"] = _Property,
	        ["ScriptType"] = _ScriptType,
	        ["New"] = _New,
	    }

	    WidgetSeparator = IGAS:NewWidget(_WidgetInfo, WidgetSeparator)
	end

	-- Script
	local _GUIWidget = IGAS.GUI.Widget

	local _FrameBackdrop = {
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true, tileSize = 16, edgeSize = 16,
		insets = { left = 3, right = 3, top = 3, bottom = 3 }
	}
	
	-- Script
	
	-- ScriptType
	local _ScriptType = {
	}

	--	Function
	local _Function = {
		-- Dispose, release resource
        ["Dispose"] = function(self)			
			-- Dispose Childs
			if self.__WidgetSeparatorNum and self.__WidgetSeparatorNum > 0 then
				for i = self.__WidgetSeparatorNum, 1, -1 do
					if self:GetChild("WidgetSeparator"..i) then
						self:GetChild("WidgetSeparator"..i):Dispose()
					end
				end
			end
			
            -- Call super's dispose
			self:CallMethod(_Base, "Dispose")
        end,
		
		-- AddWidget
		["AddWidget"] = function(self, group, widget)
			local container = self.Container
						
			if not group or type(group) ~= "string" or not widget then
				return
			end
						
			if type(widget) == "string" then
                if _GUIWidget[widget] then
                    widget = _GUIWidget[widget]
				else
					return
                end
            elseif type(widget) == "table" then
                if not widget["Function"] then
                    return
                end
            else
                return
            end

			if container.__WidgetSeparatorNum and container.__WidgetSeparatorNum > 0 then
				for i = 1, container.__WidgetSeparatorNum do
					if container:GetChild("WidgetSeparator"..i).Text == group then						
						return container:GetChild("WidgetSeparator"..i):AddWidget(widget)
					end
				end
			end
			
			local separator = CreateFrame(WidgetSeparator, nil, container)
			separator.Text = group
			
			return separator:AddWidget(widget)
		end,
		-- RemoveWidget
		["RemoveWidget"] = function(self, group, widget)
			local container = self.Container
			
			if not group or type(group) ~= "string" or not widget then
				return
			end
			
			if type(widget) == "string" then
                if _GUIWidget[widget] then
                    widget = _GUIWidget[widget]
				else
					return
                end
            elseif type(widget) == "table" then
                if not widget["Function"] then
                    return
                end
            else
                return
            end
			
			if container.__WidgetSeparatorNum and container.__WidgetSeparatorNum > 0 then
				for i = 1, container.__WidgetSeparatorNum do
					if container:GetChild("WidgetSeparator"..i).Text == group then
						container:GetChild("WidgetSeparator"..i):RemoveWidget(widget)
						return
					end
				end
			end
		end,
		-- RemoveGroup
		["RemoveGroup"] = function(self, group)
			local container = self.Container
			
			if not group or type(group) ~= "string" then
				return
			end
			
			if container.__WidgetSeparatorNum and container.__WidgetSeparatorNum > 0 then
				for i = 1, container.__WidgetSeparatorNum do
					if container:GetChild("WidgetSeparator"..i).Text == group then
						container:GetChild("WidgetSeparator"..i):Dispose()
						return
					end
				end
			end
		end,
		-- GetGroup
		["GetGroup"] = function(self, group)
			local container = self.Container
			
			if not group or type(group) ~= "string" then
				return
			end
			
			if container.__WidgetSeparatorNum and container.__WidgetSeparatorNum > 0 then
				for i = 1, container.__WidgetSeparatorNum do
					if container:GetChild("WidgetSeparator"..i).Text == group then
						return container:GetChild("WidgetSeparator"..i)
					end
				end
			end
		end,
		-- DisSelected
		["DisSelected"] = function(self)
			if selBtn then
				selBtn:Fire("OnMouseDown")
			end
		end,
	}

	--	Property
	local _Property = {
        -- Container
        ["Container"] = {
			["Get"] = function(self)
				return self.ScrollForm.Container
			end,
			
			["Type"] = IGAS:GetPropertyType("Object"),
        },
		-- SelWidget
		["SelectedWidget"] = {
			["Get"] = function(self)
				return selBtn and selBtn.Widget
			end,
			
			["Type"] = IGAS:GetPropertyType("Object"),
		},
	}

	local function _New(name, parent)
		local frame = CreateFrame(_Base, name, parent)
		
		frame.Width =200
		frame.Height = 400
		
		frame.Movable = true
		frame.Resizable = true
		frame.FrameStrata = "DIALOG"
		frame.Toplevel = true
		--frame.MouseEnabled = true
		frame.MouseWheelEnabled = true
		
		frame:SetBackdrop(_FrameBackdrop)
		frame:SetBackdropColor(0,0,0,1)
		
		local scrollForm = CreateFrame("ScrollForm", "ScrollForm", frame)
		scrollForm:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, -10)
		scrollForm:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 10)
		scrollForm:SetBackdrop(nil)
		scrollForm.ValueStep = 26
		scrollForm.Container.Height = 26
				
		return frame
	end	

	-- Register
	local _WidgetInfo = {
		["WidgetName"] = _WidgetName,
		["Base"] = _Base,
		["Function"] = _Function,
		["Property"] = _Property,
		["ScriptType"] = _ScriptType,
		["New"] = _New,
		-- Special
		["WidgetButton"] = WidgetButton,
		["WidgetSeparator" ] = WidgetSeparator,
	}

	Widget.WidgetList = IGAS:NewWidget(_WidgetInfo)
end
